package com.greenhouse9.prog07;
import java.io.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.util.ArrayList;

//import org.apache.xerces.parsers.*;

public class DOMSample1 {

    public static void main(String [] args) {

        String file = args[0];
        ArrayList<Item> itemList = new ArrayList<Item> ();

        try {
            /*
            DOMParser parser = new DOMParser ();
            parser.parse(file);
            Document doc = parser.getDocument ();*/

            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dbfactory.newDocumentBuilder();

            Document doc = builder.parse(new File(file));
            Element root = doc.getDocumentElement();

            NodeList listHit = root.getElementsByTagName("Hit");
            // System.out.println("Node Count: " + listHit.getLength()); // for debug

            for (int i = 0; i < listHit.getLength() ; i++) {
                System.out.println("Node " + i + ": " + listHit.item(i).getNodeName());  // for debug
                
                XmlObject xo = new XmlObject();
                xo.setXmlInfo(listHit.item(i));
                xo.printR(0);  // for debug

                NodeList list2 = listHit.item(i).getChildNodes();

                Item item = new Item ();

                item.yahooItemInfo = xo;
                item.init();

                for (int j = 0; j < list2.getLength(); j++) {
                    String node =  list2.item(j).getNodeName();
                    Node ndName = list2.item(j).getFirstChild();

                    // System.out.println("    Child " + j + ": " + node); // for debug

                    if ( "Name".equals(node)) {
                        item.name   = ndName.getNodeValue();
                        System.out.println("    Name " + ndName.getNodeValue());
                    }
                    if ( "Description".equals(node)) {
                        if (ndName != null) {
                            item.description   = ndName.getNodeValue();
                        }
                        //System.out.println("description " + ndName.getNodeValue());
                    }
                    if ( "Headline".equals(node)) {
                        if (ndName != null) {
                            item.headline   = ndName.getNodeValue();
                        }
                        else {
                            item.headline   = "";
                        }
                        System.out.println("Headline " + item.headline);
                    }
                    if ( "Url".equals(node)) {
                        System.out.println("Url " + ndName.getNodeValue());
                        item.url = ndName.getNodeValue();
                    }
                    if ( "Availability".equals(node)) {
                        item.availability   = ndName.getNodeValue();
                        System.out.println("Availability " + ndName.getNodeValue());
                    }
                    if ( "Code".equals(node)) {
                        System.out.println("Code " + ndName.getNodeValue());
                        item.code = ndName.getNodeValue();
                    }
                    if ( "PersonId".equals(node)) {
                        if (ndName != null){
                            item.personId   = ndName.getNodeValue();
                        }
                        else {
                            item.personId   = "";
                        }
                        System.out.println("personId " + item.personId);
                    }
                    if ( "ProductId".equals(node)) {
                        if (ndName != null) {
                            item.productId   = ndName.getNodeValue();
                        }
                        else {
                            item.productId = "";
                        }
                        System.out.println("productId " + item.productId);
                    }
                    if ( "Image".equals(node)) {
                        item.image   = ndName.getNodeValue();
                        System.out.println("image " + ndName.getNodeValue());
                    }
                    if ( "Review".equals(node)) {
                        item.review   = ndName.getNodeValue();
                        System.out.println("review " + ndName.getNodeValue());
                    }
                    if ( "Affiliate".equals(node)) {
                        item.affiliate   = ndName.getNodeValue();
                        System.out.println("affiliate " + ndName.getNodeValue());
                    }
                    if ( "Price".equals(node)) {
                        item.price   = ndName.getNodeValue();
                        System.out.println("price " + ndName.getNodeValue());
                    }
                    if ( "PriceLabel".equals(node)) {
                        item.priceLabel   = ndName.getNodeValue();
                        System.out.println("priceLabel " + ndName.getNodeValue());
                    }
                    if ( "Point".equals(node)) {
                        item.point   = ndName.getNodeValue();
                        System.out.println("point " + ndName.getNodeValue());
                    }
                    if ( "Shipping".equals(node)) {
                        item.shipping   = ndName.getNodeValue();
                        System.out.println("shipping " + ndName.getNodeValue());
                    }
                    if ( "Category".equals(node)) {
                        item.category   = ndName.getNodeValue();
                        System.out.println("category " + ndName.getNodeValue());
                    }
                    if ( "CategoryIdPath".equals(node)) {
                        item.categoryIdPath   = ndName.getNodeValue();
                        System.out.println("categoryIdPath " + ndName.getNodeValue());
                    }
/*
                    if ( "Brands".equals(node)) {
                        item.brands   = ndName.getNodeValue();
                        System.out.println("brands " + ndName.getNodeValue());
                    }
                    if ( "JanCode".equals(node)) {
                        //item.janCode   = ndName.getNodeValue();
                        //System.out.println("janCode " + ndName.getNodeValue());
                        if (ndName != null) {
                            item.janCode   = ndName.getNodeValue();
                        }
                        else {
                            item.janCode = "";
                        }
                        System.out.println("janCode " + item.janCode);
                    }
                    if ( "Model".equals(node)) {
                        if (ndName != null) {
                            item.model   = ndName.getNodeValue();
                        }
                        else {
                            item.model = "";
                        }
                        System.out.println("model " + item.model);
                    }
                    if ( "IsbnCode".equals(node)) {
                        if (ndName != null) {
                            item.isbnCode   = ndName.getNodeValue();
                        }
                        else {
                            item.isbnCode = "";
                        }
                        System.out.println("isbnCode " + item.isbnCode);
                    }
                    if ( "Store".equals(node)) {
                        if (ndName != null) {
                            item.store   = ndName.getNodeValue();
                        }
                        else {
                            item.store = "";
                        }
                        System.out.println("store " + item.store);
                    }
*/
                }

                itemList.add(item);
            }
        }
        catch (Exception e) {
            System.err.println(e);
        }

        /* print item List */
        System.out.println();
        Item item_temp = new Item();
        for (int i = 0; i < itemList.size(); i ++) {
            item_temp = itemList.get(i);
            System.out.println(i + ": " + item_temp.code + ": " + item_temp.name);
            System.out.println("---- item ----");
            System.out.println(i + ": " + item_temp.yahooItemInfo.name);
            System.out.println(i + ": " + item_temp.code1 + ": " + item_temp.name1);
        }

    }
    
    private static void  printNode(Node node, int indent){

        for (int i=0; i < indent ; i++){
            System.out.print(" ");
        }

        if(node.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println("[EL]" + node.getNodeName());
        }
        else {
            System.out.println("[TX]" + node.getNodeValue());
        }

        if(node.hasChildNodes()) {

            NodeList list = node.getChildNodes();

            for ( int j = 0; j < list.getLength(); j++ ) {

                printNode(list.item(j), indent + 4);

            }
        }
    }

    private static XmlObject getXmlInfo (Node node ){

        XmlObject xo = new XmlObject();

        xo.type = node.getNodeType();
        xo.name = node.getNodeName();
        xo.value = node.getNodeValue();

        ArrayList<XmlObject> vlist = new ArrayList<XmlObject> ();

        if(node.hasChildNodes()) {

            NodeList list = node.getChildNodes();

            for ( int j = 0; j < list.getLength(); j++ ) {

                //printNode(list.item(j), indent + 4);
                vlist.add(getXmlInfo(list.item(j)));

            }
            xo.child = vlist;
        }
        return xo;
    }
}

